//
//  ReadXqViewController.m
//  news
//
//  Created by dllo on 16/3/11.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "ReadXqViewController.h"

@interface ReadXqViewController ()
@property(nonatomic, assign)BOOL isPlay;// 是否收藏

@end

@implementation ReadXqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _name;
     /** 按钮 **/
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sc"] style:UIBarButtonItemStylePlain target:self action:@selector(shoucang:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
    
    self.tabBarController.tabBar.hidden = YES;
    
#pragma mark - 数据库查询
    // 查询
    NSArray *array = [[DataBaseManager shareInstance]select];
    // 判断是否收藏
    NSInteger mark = 0;
    for (Read *model in array) {
        if ([model.listenName isEqualToString:_name]) {
            // 如果收藏就加1
            mark++;
        }
    }
    // 当没有收藏
    if (mark == 0) {
        self.isPlay = YES;
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    }
    // 如果收藏
    else{
        self.isPlay = NO;
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
    }
    /** WebView **/
    UIWebView *wv = [[UIWebView alloc]initWithFrame:CGRectMake(0,  0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view addSubview:wv];
    
    NSString *str = self.docid;
    [wv loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
//    NSLog(@"%@", str);
    
    
}
#pragma mark - 收藏按钮实现
- (void)shoucang:(UIBarButtonItem *)shoucang{
    Read *m = [[Read alloc]init];
    m.listenName = _name;
    m.pubdate =_docid;
    // BOOl判断
    if (_isPlay) {
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor redColor];
     [[DataBaseManager shareInstance]insert:m];
    }else{
        // 删除
        [[DataBaseManager shareInstance]deleteMovieWithName:m];
        self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    }
    _isPlay = !_isPlay;
    
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"hhh" object:nil];
}
#pragma mark - 返回按钮
- (void)fanhui{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
